﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing;

namespace LevelEditor.Forms
{
    public sealed class CustomCursors
    {
        #region Fields

        private static Cursor pencil;
        private static Cursor bucket;
        private static Cursor eraser;
        private static Cursor picker;
        private static Cursor magnifier;

        #endregion

        #region Stuff needed to create a custom cursor

        private struct IconInfo
        {
            public bool fIcon;
            public int xHotspot;
            public int yHotspot;
            public IntPtr hbmMask;
            public IntPtr hbmColor;
        }

        [DllImport("user32.dll")]
        private static extern IntPtr CreateIconIndirect(ref IconInfo icon);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool GetIconInfo(IntPtr hIcon, ref IconInfo pIconInfo);

        private static Cursor CreateCursor(Bitmap bmp, int xHotSpot, int yHotSpot)
        {
            IconInfo iconInfo = new IconInfo();
            GetIconInfo(bmp.GetHicon(), ref iconInfo);
            iconInfo.xHotspot = xHotSpot;
            iconInfo.yHotspot = yHotSpot;
            iconInfo.fIcon = false;
            return new Cursor(CreateIconIndirect(ref iconInfo));
        }

        #endregion

        #region Properties

        public static Cursor Pencil
        {
            get
            {
                if (pencil == null)
                    pencil = CreateCursor(Properties.Resources.pencilToolCursor, 0, 20);
                return pencil;
            }
        }

        public static Cursor Bucket
        {
            get
            {
                if (bucket == null)
                    bucket = CreateCursor(Properties.Resources.bucketToolCursor, 1, 19);
                return bucket;
            }
        }

        public static Cursor Eraser
        {
            get
            {
                if (eraser == null)
                    eraser = CreateCursor(Properties.Resources.eraserToolCursor, 4, 18);
                return eraser;
            }
        }

        public static Cursor Picker
        {
            get
            {
                if (picker == null)
                    picker = CreateCursor(Properties.Resources.colorPickerToolCursor, 0, 22);
                return picker;
            }
        }

        public static Cursor Magnifier
        {
            get
            {
                if (magnifier == null)
                    magnifier = CreateCursor(Properties.Resources.magnifierCursor, 8, 8);
                return magnifier;
            }
        }

        #endregion
    }
}
